package edu.gatech.cs2340.OregonTrail;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.io.IOException;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JButton;
import edu.gatech.cs2340.OregonTrail.EventSystem.AtShopEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.CommCoordinator;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class LocationPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	
	
	//private OregonTrailApp parentFrame;
	//private Location location;
	private Image image;
	//private JPanel panel;
	//private CommCoordinator communicator;
	//private Party party;
	
	
	public LocationPanel(final OregonTrailApp parentFrame, final Location location, final CommCoordinator communicator, final Party party){
		//this.parentFrame = parentFrame;
		//this.location = location;
		//this.communicator = communicator;
		//this.party = party;
		//this.panel = this;
		try {
			image = ImageIO.read(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/background.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setPreferredSize(new Dimension(524, 600));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{20, 227, 226, 20, 0};
		gridBagLayout.rowHeights = new int[]{15, 25, 435, 25, 25, 25, 15, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel locationName = new JLabel("");
		locationName.setText(location.getLocationName());
		GridBagConstraints gbc_locationName = new GridBagConstraints();
		gbc_locationName.gridwidth = 2;
		gbc_locationName.insets = new Insets(0, 0, 5, 5);
		gbc_locationName.gridx = 1;
		gbc_locationName.gridy = 1;
		add(locationName, gbc_locationName);
		
		JButton btnRest = new JButton("Rest");
		btnRest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(null,"Implement resting");
			}
		});
		GridBagConstraints gbc_btnRest = new GridBagConstraints();
		gbc_btnRest.insets = new Insets(0, 0, 5, 5);
		gbc_btnRest.gridx = 1;
		gbc_btnRest.gridy = 3;
		add(btnRest, gbc_btnRest);
		
		JButton btnTrade = new JButton("Trade");
		btnTrade.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null,"Implement trading");
			}
		});
		GridBagConstraints gbc_btnTrade = new GridBagConstraints();
		gbc_btnTrade.insets = new Insets(0, 0, 5, 5);
		gbc_btnTrade.gridx = 2;
		gbc_btnTrade.gridy = 3;
		add(btnTrade, gbc_btnTrade);
		
		JButton btnLeave = new JButton("Leave");
		btnLeave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				parentFrame.showPlayPanel();
			}
		});
		GridBagConstraints gbc_btnLeave = new GridBagConstraints();
		gbc_btnLeave.insets = new Insets(0, 0, 5, 5);
		gbc_btnLeave.gridx = 1;
		gbc_btnLeave.gridy = 4;
		add(btnLeave, gbc_btnLeave);
		
		if (location.hasShop()){
			JButton btnShop = new JButton("Shop");
			btnShop.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					communicator.ThrowGUIEvent(new AtShopEvent(location.getShop(), location.getShop().getShopPrices(), party.getPlayer().getInventory().getStock(), party.getPlayer().getMoney(),
							party.getPlayer().getInventory().getCurrentWeight()));
				}
			});
			GridBagConstraints gbc_btnShop = new GridBagConstraints();
			gbc_btnShop.insets = new Insets(0, 0, 5, 5);
			gbc_btnShop.gridx = 2;
			gbc_btnShop.gridy = 4;
			add(btnShop, gbc_btnShop);	
		}
		
		JButton btnMenu = new JButton("Menu");
		btnMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				parentFrame.showMenu();
			}
		});
		GridBagConstraints gbc_btnMenu = new GridBagConstraints();
		gbc_btnMenu.gridwidth = 2;
		gbc_btnMenu.insets = new Insets(0, 0, 5, 5);
		gbc_btnMenu.gridx = 1;
		gbc_btnMenu.gridy = 5;
		add(btnMenu, gbc_btnMenu);
	}

	public void paintComponent(Graphics g) {
	    super.paintComponent(g);

	    // Draw the background image.
	    g.drawImage(image, 0, 0, null);
	  }
}